//
//  HHThemeViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/10.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHThemeViewController.h"

@interface HHThemeViewController ()
@property(nonatomic, strong) NSArray *themeArr;
@end

static NSString *const cellID = @"cellID";
@implementation HHThemeViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(themeDidChange) name:kThemeDidChangeNotification object:nil];
    
    [self setupTableViewAndData];
}
#pragma mark - setupTable And Data
-(void)setupTableViewAndData{
    self.themeArr =[[ThemeManager shareManager].themeDic allKeys];
    
    self.tableView.tableHeaderView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];
    self.tableView.tableFooterView =[UIView new];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
}
#pragma mark - dealloc
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - SEL Method
-(void)themeDidChange{
    
    self.tableView.backgroundColor =[[ThemeManager shareManager]loadColorWithKeyName:@"mask_bg@2x.jpg"];
    
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.themeArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor =[UIColor clearColor];
    
    cell.textLabel.text = self.themeArr[indexPath.row];
    
    cell.imageView.image =[[ThemeManager shareManager]loadThemeImageWithThemeName:self.themeArr[indexPath.row]];
    
    if ([self.themeArr[indexPath.row] isEqualToString:[ThemeManager shareManager].themeName]) {
        
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [ThemeManager shareManager].themeName = self.themeArr[indexPath.row];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:kThemeDidChangeNotification object:nil];
    
    [tableView reloadData];
    
}
@end
